/*
* Copyright 2011 Andy Lee.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package uk.co.optimisticpanda.remoteweb.command

import groovy.lang.Closure

import java.util.List

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement

class Command {

	List<Command> children = []
	String description
	private Executable executable

	public void execute(WebDriver driver){
		if (executable) executable.call driver
	}

	@Override
	public String toString() {
		return "description=" + description + ", executable=" + executable + ", size of children:" + children.size + ",children:" + children.collect{"\t" + it} ;
	}
	
	public boolean isContainer(){
		children
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((children == null) ? 0 : children.hashCode());
		result = prime * result
				+ ((description == null) ? 0 : description.hashCode());
		result = prime * result
				+ ((executable == null) ? 0 : executable.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this.is( obj))
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Command other = (Command) obj;
		if (children == null) {
			if (other.children != null)
				return false;
		} else if (!children.equals(other.children))
			return false;
		if (description == null) {
			if (other.description != null)
				return false;
		} else if (!description.equals(other.description))
			return false;
		if (executable == null) {
			if (other.executable != null)
				return false;
		} else if (!executable.equals(other.executable))
			return false;
		return true;
	}

}

class Executable {

	Closure closure
	def variableFinder
	
	public void call(arg){
		closure(arg)
	}

}


